package com.zw.weiyi.avtivity;

import java.io.InputStream;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zw.weiyi.enety.Person;
import com.zw.weiyi.phonesms.BaseActivity;
import com.zw.weiyi.phonesms.R;
import com.zw.weiyi.util.PhoneUtil;

public class InfoActivity extends BaseActivity {
	private TextView p_infophont, p_infonames;
	private ImageView p_click_phone, p_click_sms, p_im_infoperson,back,info;
	private Person person;
	private ContentResolver resolver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info_person);
		initview();

	}

	public void initview() {
		person = new Person();
		resolver = getContentResolver();
		
		Intent intent = this.getIntent();
		person.setName(intent.getStringExtra("name"));
		
		person.setPhoneNumber(intent.getStringExtra("phoneNumber"));
		person.setIconId(intent.getLongExtra("iconid", 0));
		person.setId(intent.getLongExtra("id", 0));

		p_infophont = (TextView) findViewById(R.id.p_infophont);
		p_infonames = (TextView) findViewById(R.id.p_infonames);
		p_click_phone = (ImageView) findViewById(R.id.p_click_phone);
		p_click_sms = (ImageView) findViewById(R.id.p_click_sms);
		p_im_infoperson = (ImageView) findViewById(R.id.p_im_infoperson);
		back = (ImageView) findViewById(R.id.back);
		info = (ImageView) findViewById(R.id.info);
		
		if (person.getIconId() > 0) { // 头像默认为0
			Uri uri = ContentUris.withAppendedId(
					ContactsContract.Contacts.CONTENT_URI,
					person.getId());
			InputStream input = ContactsContract.Contacts
					.openContactPhotoInputStream(resolver, uri);
			person.setIcon(BitmapFactory.decodeStream(input));

		} else {// 没有则默认
			person.setIcon(BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_launcher));
		}
		p_im_infoperson.setImageBitmap(person.getIcon());
		p_infonames.setText(person.getName());
		p_infophont.setText(person.getPhoneNumber());
		//拨打电话
		p_click_phone.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				 PhoneUtil.phoneNumber(InfoActivity.this,  person.getPhoneNumber());
			}
		});
		
		//发短信
		p_click_sms.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(InfoActivity.this,InfoSMSActivity.class);		
				intent.putExtra("number", person.getPhoneNumber());	
				intent.putExtra("name", person.getName());	
				startActivity(intent);		
			}
		});
		
		//返回
		back.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				finish();				
			}
		});
		//更多操作
		info.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				//需要吧数据传过去
				Intent intent = new Intent(InfoActivity.this,DialogActivity.class);		
				intent.putExtra("did", person.getId());	
				intent.putExtra("name", person.getName());
				intent.putExtra("number", person.getPhoneNumber());	
				startActivity(intent);				
			}
		});
	}
}
